package com.mxx.common.netty.http;

import io.netty.handler.codec.http.HttpResponseStatus;

import java.io.Serializable;

/**
 * @Auther: Mr. Zeng
 * @Date: 2020/8/3 19:10
 * @Description: 暂无描述
 */
public class DefaultNettyHttpResponseTemplate implements NettyHttpResponseTemplate {

    private final String CONTENT_NORMAL = "{\"code\":200,\"msg\":\"SUCCESS\"}";
    private final String CONTENT_ERROR = "{\"code\":%s,\"msg\":\"%s\"}";


    @Override
    public Serializable with(HttpResponseStatus status) {
        if (status.code() == 200) {
            return CONTENT_NORMAL;
        } else {
            return String.format(CONTENT_ERROR, status.code(), status.reasonPhrase());
        }

    }

    @Override
    public Serializable with(Throwable throwable) {
        return String.format(CONTENT_ERROR, 500, throwable.getClass().getName() + ":" + throwable.getMessage());
    }


}
